/**
 * Copyright (c) 2018 小润科技 All rights reserved.
 * <p>
 * 版权所有，侵权必究！
 */

package com.xr.sy.modules.business.controller;

import com.xr.common.annotation.LogOperation;
import com.xr.common.constant.Constant;
import com.xr.common.page.PageData;
import com.xr.common.utils.Result;
import com.xr.common.validator.AssertUtils;
import com.xr.common.validator.ValidatorUtils;
import com.xr.common.validator.group.AddGroup;
import com.xr.common.validator.group.DefaultGroup;
import com.xr.common.validator.group.UpdateGroup;
import com.xr.sy.modules.business.dto.NoticeDTO;
import com.xr.sy.modules.business.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.Map;

/**
 * 公告管理
 */
@RestController
@RequestMapping("business/notice")
@Api(tags = "公告管理")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "title", value = "标题", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startDate", value = "开始时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endDate", value = "结束时间", paramType = "query", dataType = "String")
    })
    public Result<PageData<NoticeDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageData<NoticeDTO> page = noticeService.page(params);
        return new Result<PageData<NoticeDTO>>().ok(page);
    }

    @GetMapping("latest")
    public Result<NoticeDTO> latest() {
        NoticeDTO dto = noticeService.latest();
        return new Result<NoticeDTO>().ok(dto);
    }

    @ApiOperation("信息")
    @GetMapping("{id}")
    public Result<NoticeDTO> info(@PathVariable("id") Long id) {
        NoticeDTO notice = noticeService.get(id);
        return new Result<NoticeDTO>().ok(notice);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    @RequiresPermissions("business:notice:all")
    public Result save(@RequestBody NoticeDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        noticeService.save(dto);
        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    @RequiresPermissions("business:notice:all")
    public Result update(@RequestBody NoticeDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        noticeService.update(dto);
        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    @RequiresPermissions("business:notice:all")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");
        noticeService.deleteBatchIds(Arrays.asList(ids));
        return new Result();
    }

}